package com.tomehay.termprojectmanage_server.controller;

import com.tomehay.termprojectmanage_server.pojo.Model.PersonalTask;
import com.tomehay.termprojectmanage_server.pojo.Model.Plan;
import com.tomehay.termprojectmanage_server.pojo.VO.PersonalTaskVO;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.PersonalTaskService;
import com.tomehay.termprojectmanage_server.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * (PersonalTask)控制层
 *
 * @author makejava
 * @since 2023-06-24 13:40:21
 */
@RestController
@RequestMapping("/api/personal-task")
public class PersonalTaskController {
    /**
     * 服务对象
     */
    @Resource
    private PersonalTaskService personalTaskService;

    @Resource
    private PlanService planService;

    @RequestMapping("/save")
    public boolean save(@RequestBody PersonalTask personalTask){
        personalTaskService.savepersonalTask(personalTask);
        return true;
    }

    @GetMapping("/list")
    public List<PersonalTask> list(@RequestParam("id") Long id) {
        return personalTaskService.list(id.intValue());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param personalTask 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public ResultVO<PersonalTask> selectOne(PersonalTask personalTask) {
        PersonalTask result = personalTaskService.selectById(personalTask.getId());
        if(result != null){
            ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 新增一条数据
     *
     * @param personalTask 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ResultVO<PersonalTask> insert(@RequestBody PersonalTask personalTask) {
        int result = personalTaskService.insert(personalTask);
        if (result > 0) {
            ResultVO.createSuccessResponse(personalTask);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 修改一条数据
     *
     * @param personalTask 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResultVO<PersonalTask> update(@RequestBody PersonalTask personalTask) {
        PersonalTask result = personalTaskService.update(personalTask);
        if (result != null) {
            ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 删除一条数据
     *
     * @param personalTask 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ResultVO<PersonalTask> delete(PersonalTask personalTask) {
        int result = personalTaskService.deleteById(personalTask.getId());
        if (result > 0) {
            return ResultVO.createSuccessResponse(null);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public ResultVO<List<PersonalTask>> selectAll() {
        List<PersonalTask> personalTasks = personalTaskService.selectAll();
        if (personalTasks != null) {
            ResultVO.createSuccessResponse(personalTasks);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public ResultVO<List<PersonalTask>> selectPage(Integer start, Integer limit) {
        List<PersonalTask> personalTasks = personalTaskService.selectPage(start, limit);
        if (personalTasks != null) {
            return ResultVO.createSuccessResponse(personalTasks);
        }
        return ResultVO.createErrorResponse();
    }

    @PostMapping("/divide")
    public void divideTask(@RequestBody List<Plan> list) {
        planService.divide(list);
    }

    @GetMapping("/getOne")
    public ResultVO<PersonalTask> getDetailById(@RequestParam("id") Long id) {
        PersonalTask detailById = personalTaskService.getDetailById(id.intValue());
        if (detailById != null) {
            return ResultVO.createSuccessResponse(detailById);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 查询当前课程中当前大作业的全部个人任务
     *
     * @return Response对象
     */
    @GetMapping("/selectAllPersonalTask")
    public ResultVO<List<PersonalTask>> selectAllPersonalTask(@RequestParam("projectId") Integer pid, @RequestParam("groupId") Integer gid, @RequestParam("gmId") Integer gmId) {
        List<PersonalTask> personalTasks = personalTaskService.selectAllPersonalTask(pid, gid, gmId);
        if (personalTasks != null) {
            return ResultVO.createSuccessResponse(personalTasks);
        }
        return ResultVO.createErrorResponse();
    }

    @PostMapping("/UpLoadCode")
    public ResultVO<Void> UpLoadCode(@RequestParam(value = "file") MultipartFile multipartFile,
                                     @RequestParam(value = "id") int id) throws IOException {
        return personalTaskService.UpLoadCode(multipartFile,id);
    }

    @PostMapping("/UpLoadDoc")
    public ResultVO<Void> UpLoadDoc(@RequestParam(value = "file")MultipartFile multipartFile,
                                    @RequestParam(value = "id") int id) throws IOException {
        return personalTaskService.UpLoadDoc(multipartFile,id);
    }

    @DeleteMapping("/DeleteCode")
    public ResultVO<Void> DeleteCode(int id,String path){
        return personalTaskService.DeleteCode(id,path);
    }

    @DeleteMapping("/DeleteDoc")
    public ResultVO<Void> DeleteDoc(int id,String path){
        return personalTaskService.DeleteDoc(id,path);
    }

    @GetMapping("/FinishGroupTask")
    public ResultVO<Void> FinishGroupTask(int id){
        return personalTaskService.FinishGroupTask(id);
    }


    @GetMapping("/GetByGroupTaskID")
    public ResultVO<List<PersonalTaskVO>> GetByGroupTaskID(int id){
        return personalTaskService.GetByGroupTaskID(id);
    }


}

